<?php

// Check if lock file exists.

$filename = './lock.php';

if (file_exists($filename))
{
	die ("Error: You have already installed MyBW. If you need to reinstall it, maybe to reset the mysql connection detials, please delete the 'lock.php' file in the install directory.");
}
else // If the lock file doesn't exist, continue with the install.
{
}

include('../mysql.php');
include('../config.php');

$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];

//Connect to mysql server
mysql_connect($dbhost,$dbuser,$dbpass);

//Select db
mysql_select_db($dbname);

//Query to run
$query = "INSERT INTO users (username,password,email,theme,gid) VALUES ('$username','$password','$email','default','0')";

//Run query
$result = mysql_query($query);

// Finally, create the lock file to lock the installer.

$FileName = "./lock.php";
$FileHandle = fopen($FileName, 'w');
fclose($FileHandle);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
		<link type="text/css" href="./styles.css" rel="stylesheet" media="screen" />

		<title>Install MyBW - Finish Installation</title>
	</head>

	<body>
		<div id="container">
			<div id="header">
				<img src="../inc/images/logo-300-up.png" alt="MyBW" />
			</div>
			
				<div id="foxmenucontainer">
					<div id="foxmenu">
						<ul>
							<li><a href="#">MySQL Details</a></li>
							<li><a href="#">Default Admin Details</a></li>
							<li><a href="#" class="current">Finish</a></li>
						</ul>
					</div>
				</div>
				
			<div id="content">				
		<p><h3>Finish Installation</h3>
		
		<?php
		
		//Connect to mysql server
		mysql_connect($dbhost,$dbuser,$dbpass);

		//Select db
		mysql_select_db($dbname);

		//Query to run - create db's, etc...
$query1 = "CREATE TABLE IF NOT EXISTS `mybw_notes` (
  `id` int(1) NOT NULL default '1',
  `notes` varchar(225) NOT NULL,
  `save_author` varchar(225) NOT NULL,
  `save_date` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;";

mysql_query($query1) or die('Error:' . mysql_error() . '');

$query2 = "CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL auto_increment,
  `username` varchar(225) NOT NULL,
  `password` varchar(225) NOT NULL,
  `email` varchar(225) NOT NULL,
  `theme` varchar(225) NOT NULL,
  `gid` int(4) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6;";

mysql_query($query2) or die('Error:' . mysql_error() . '');

$query3 = "INSERT INTO `mybw_notes` (`id`, `notes`, `save_author`, `save_date`) VALUES
(1, 'These are your notes...', 'bob', '2009-07-16 05:36:30');";

		//Run query
		mysql_query($query3) or die('Error:' . mysql_error() . '');
		
		echo "Creating table mybw_notes...........Done <br />";
		echo "Creating table users...........Done <br />";
		echo "Inserting default data...........Done";
		
		?>
		<br />
		<br />
		<b>Installation complete!</b><br />
		You can now <a href="../index.php">log in to your control panel</a>...</p>

			</div>
			<div id="footer">
				<a href="http://mybw.co.uk/forums/showthread.php?tid=16">Installation</a> &nbsp; &nbsp; 
				<a href="http://mybw.co.uk/forums/">Support Forums</a> &nbsp; &nbsp; 
				<a href="http://mybw.co.uk/license.php">License</a>
			</div>
		</div>
	</body>
</html>
